package com.kirayous.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.kirayous.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id，这里用的是沙箱环境的appid
    private   String app_id = "2021000119665986";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCrpq6er/vwgVgLS6U/oLfbj9Mo8/xPO75yUBshtqB9E0bZxwUUnvVnMNM5C2Fx1DZ7eAJFDGGeEwAdYb49jv7xwcORSXSuhdHcPwYHMgTaoIJQsklY/urBgtS7oyRf7AFue8+2ZItIChN96NIicl5+V+lfsxgt2vCUODWZ2slq/P4lBcHlHxntc/782akKYEsFBnOUcM0UIyF4RNfUH8pWSPNpJzF8eFPKsvCQ9VeJbSRvJupiXmc20Puj6gpIFV/gldY3k2X/5opZtKwQIBsfEotih80PcHnN81RKgcGSIByY3FHYenSOct6dkW+HBs+nEBVENdciIEf02BcRGYRHAgMBAAECggEAFGs7knz0x2Nbidxxx+Pieyfcwp5jgTlmKksFLzl+VeKZJwbZv9446xGPztVfvh60bHOWMYiLhDZWaShbbQ85nKCkFwBZ9PjOQhjtKqX0ld1KIzdSKlgIrQRXMgryJMb6VJ4900CaoPMx5mRvutFIFFd4132CftHwlHErTyYLl8JWyErr31nvuMjEDVMntRUSuffqJ4PZUUzx75hh5YEl7lASu9C9zQL2tvPYmbUWmhoIW1ZQmcxnqtV4McqMMWsMald+nL1W/tfox0l7WwSYaS5wpDRJiROgxmK011XFP1DQaaSR+JzHaoyWLgZhQ/hL6qWbV4duZG6KENfp9oFQgQKBgQDUvQoOHJMdEKlXwTYqoZfU7mJgSt/+76pLj0jjh6hDZXDgPg9bCko7h5cewG8upNTMeAwrewVJ9uXJcAv5L5VQ9RKI17el/zftCm2jUmfY06Ksrin9uZG+muX/1w79NcThpeuTS6mcCkI7vrvQ1W3OdAiiUmZcOpS+krI6CQo48QKBgQDOjqzjIDqU4DDQzVTIFqn+l4oQapmUDFxbmyYzJzHuMvp0PTyP/DQDtW4PGoy3kKRY1vetbjX+7ta8zO5J+JtzOaszfa0hMfldXnCVy0AXz3sbK3hJDTITy02Vy8K9XROnXOg4JAo+mM/OyWf3aKW/0m/wFVCqMdu1iXzbHPfQtwKBgQDFU3dhMYYPnqXE/TV2A59m5ZsggDMXME0Aey57xhgAM7aoeUgbBQdoAlPdNVerpz6vTebT6e809SudVi30+/r/NeL0RUtWUM4aM+MQa95hU6T/kSjn7NnQN7OBRITlv5sYRqOlY7g2QZm/+yIRCfcpWZ2smsvRXpEb18wzYrE1sQKBgHCX/U3hZ413l6R6CNj44blRyYShVw4Int+gdvYzS1hYmYV7S+QAyimrYE8DXelCAxQaOfBjvzMz5gIMg2f3CGWK7fOnchRCNXfb3usIW+uewbBRxcOsWbvIeoZJB+JJNklor3zKyHrOuZgy2WVXueISR8pLFClx/B280D5wT/WJAoGBAMmVSpT0nsn3ywDlPRG1F6eQ90KgehlM2wHto2bmBhk5szge7gloXY3U1TRdr1tUsPk9cRLdNjKodWhFF1Ftz2FGX8NZgE1VRQaQOhVrgsQafXrIMuY7kBWetkK0dwLwUYYXRgxJZI43qV++8x4t8F+MInCzyMg/SkNp2GgUx2DY";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhP8LQHPX/VsgEwI8GdAEHC69aDgctKYpjOCFJbzVNHWmvmQ+wjAp9qr+W2CpRAULhL0s86JZsx/YYkFmLE6XVSoqzBpTXI747pYCy03v7kYJb+Qyp6VUi/Ilps1ART/s9KB11goEQKTf6apUwgKbn5gkuvWr3BsMSw9ZF6d0GTUH8wN71CeO0coxf8XFqguENLbTvYL+y2rSWHQ1e0D8pHUQkwkBDduXQBDiN3zZT/HJGfWfdTERSNhoGuHa7VIaKLfpEVAigUzlNm1+xTepPjnIbNdYP4zUz/MxSTeoRybFT+qn78dg5tajp/JgI+TKrR8eDu1PZU5wHgcZgPfikwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息，也就是会每隔几秒会发送消息给我们
//    private  String notify_url = "http://497n86m7k7.52http.net/payed/notify";
    private  String notify_url = "http://1.14.151.242/local/order/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://1.14.151.242/local/member/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
